package util;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.TimeZone;

import javax.ejb.Stateless;

@Stateless
public class DateUtil {
	
	public Date change2UTC(Date date) {
		Instant instant = Instant.ofEpochMilli(date.getTime());
		ZoneId utc = ZoneId.of("UTC");
		LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
		System.out.println("1"+dateTime.toString());
		
		ZonedDateTime utcDateTime = ZonedDateTime.of(dateTime, utc);
		System.out.println("2"+utcDateTime.toString());
		
		return null;
    }
	
	public Date addSeconds(Date date, Integer seconds) {
		Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.SECOND, seconds);
        return cal.getTime();
    }
	
	public Date round2Month(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);		
		Calendar out = Calendar.getInstance();
		out.set(Calendar.YEAR, cal.get(Calendar.YEAR));
		out.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        return out.getTime();
    }
	
	public Date round2Day(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

	public Date round2Hour(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
	
	public Date startOfDay(Date date) {
		Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
        return cal.getTime();
    }
	
	public Date endOfDay(Date date) {
		Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
        return cal.getTime();
    }
	
	public String splitDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(date);

    }
	
	public String splitTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);

    }
	
	public Date firstTimeOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
        return cal.getTime();
    }
	
	public Date lastTimeOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, cal.getActualMaximum(Calendar.MILLISECOND));
        return cal.getTime();
    }
	
	
	public Date customDate(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second, Integer milisecond) {
		Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, second);
        cal.set(Calendar.MILLISECOND, milisecond);
        return cal.getTime();
    }
	
	public String timeFormat(Integer seconds) {
		long longVal = seconds.longValue();
	    int hours = (int) longVal / 3600;
	    int remainder = (int) longVal - hours * 3600;
	    int mins = remainder / 60;
	    remainder = remainder - mins * 60;
	    int secs = remainder;

	    String time = "0 sec";
	    
	    if(hours!=0){
	    	time = hours+"h";
	    }
	    if(mins!=0){
	    	time = time+mins+"m";
	    }
	    if(secs!=0){
	    	time = secs+"s";
	    }
	    		
        return time;
    }
	
	
	public String getMonthName(Integer month){
		LocalDate localDate = LocalDate.of(0, month, 0);
	    return localDate.getMonth().name();
	}
	
	public String getMonthName(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);		
		LocalDate localDate = LocalDate.of(0, cal.get(Calendar.MONTH)+1, 1);
		return localDate.getMonth().name();
	}
	
	public Integer getYear(Date date){
		TimeZone zone = TimeZone.getTimeZone("Etc/GMT"); 
		Calendar cal = Calendar.getInstance(zone);
		cal.setTime(date);		
		return cal.get(Calendar.YEAR);
	}
	
	public LinkedHashMap<String,Integer> getDateMap(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);		
		LinkedHashMap<String,Integer> datemap = new LinkedHashMap<String,Integer>();
		datemap.put("year", cal.get(Calendar.YEAR));
		datemap.put("month", cal.get(Calendar.MONTH));
		datemap.put("day", cal.get(Calendar.DAY_OF_MONTH));
		datemap.put("hour", cal.get(Calendar.HOUR_OF_DAY));
		datemap.put("minute", cal.get(Calendar.MINUTE));
		datemap.put("second", cal.get(Calendar.SECOND));
		datemap.put("millisecond", cal.get(Calendar.MILLISECOND));
		
		return datemap;
		
	}
	
	
	

}
